package tianwang.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import tianwang.services.integrate.StanfordWordSegmentor;
import tianwang.services.integrate.WordSegmentor;


public class WordSegmentorUtil {
	
	private static StanfordWordSegmentor segmentor = null;
	
	public static List<String> getWordsFromText(String text,boolean ifWhiteSpace){
		text = text.trim();
		if(text.equals(""))
			return null;
		
		if(segmentor == null){
			segmentor = new StanfordWordSegmentor();
		}
		
		BufferedReader reader = new BufferedReader(new StringReader(text));
		String line = null;
		List<String> result = new ArrayList<String>();
		try {
			while((line = reader.readLine()) != null){
				if(line.trim().equals("")){
					continue;
				}
				
				result.addAll(ifWhiteSpace?segmentor.segregateWithWhitespace(line):segmentor.segregateWithPTBTokenizer(line));
			}
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		
		return result;
		
	}
}
